package cn.ac.iie.di.postbar.util;

import static cn.ac.iie.di.postbar.server.PostQGLtStartUp.trsclientip;
import com.trs.ckm.soap.ABSHold;
import com.trs.ckm.soap.ABSResult;
import com.trs.ckm.soap.CkmSoapException;
import com.trs.ckm.soap.PLOResult;
import com.trs.ckm.soap.RuleCATField;
import com.trs.ckm.soap.TrsCkmSoapClient;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.log4j.Logger;

public class TRSTools {

    //test
//    private String trsclientip = "http://10.136.65.134:8000;http://10.136.65.135:8000;http://10.136.65.136:8000;http://10.136.65.137:8000;http://10.136.65.138:8000;http://10.136.65.139:8000;http://10.136.65.140:8000;http://10.136.65.141:8000;http://10.136.65.142:8000;http://10.136.65.143:8000;http://10.136.65.144:8000;http://10.136.65.145:8000;http://10.136.65.146:8000;http://10.136.65.147:8000;http://10.136.65.148:8000;http://10.136.65.149:8000;http://10.136.65.150:8000;http://10.136.65.51:8000";
//    private String trsclientip = "http://192.168.11.124:8400";
    private String trsUrls;
    private TrsCkmSoapClient client;
    private static Logger logger = Logger.getLogger(TRSTools.class.getName());

    public TRSTools() {
        this.trsUrls = trsclientip;
    }

    private void init() {
        this.client = new TrsCkmSoapClient();
        logger.debug("can use trsurl :" + this.trsUrls);
        this.client.SetServer(this.trsUrls, "admin", "trsadmin");
    }

    /**
     * 得到可用的trsUrl 如果不可用就移除 如果集合中的url少于10那么就从新赋值配置信息
     *
     * @param urlString
     * @param urlD
     * @return
     */
    public String getTrsUrl(String urlString, String urlD) {
        List urlList = new ArrayList();
        String[] urlStrings = urlString.split(";");
        for (String url : urlStrings) {
            urlList.add(url);
        }
        if (urlList.size() < 10) {
            this.trsUrls = getTrsUrl(trsclientip, null);
            return this.trsUrls;
        }
        urlList.remove(urlD);
        logger.info("remove the ip is " + urlD);
        String urlLists = urlList.toString().replaceAll(", ", ";");
        urlLists = urlLists.substring(1, urlLists.length() - 1);
        logger.debug("now ,the trs client ip is " + urlLists);
        return urlLists;
    }

    public String[] getMKeyWordList(String cont) {
        while (true) {
            if (this.client == null) {
                init();
            }
            try {
                ABSHold absHold = new ABSHold(5, 10);
                ABSResult absResult = this.client.ABSText(cont, absHold);
                String keywordStr = absResult.getwordlist();
                if ("".equals(keywordStr) || keywordStr == null) {
                    return new String[]{};
                } else {
                    return keywordStr.split(";");
                }
            } catch (CkmSoapException ex) {
                logger.error("trserror  and continue" + ex);
                String errorStr = ex.toString();
                if (errorStr.contains("http")) {
                    String urlD = errorStr.substring(errorStr.indexOf("http"), errorStr.indexOf("8000") + 4);
                    this.trsUrls = getTrsUrl(this.trsUrls, urlD);
                    this.client = null;
                }
            } catch (Exception e) {
                if (this.client == null) {
                    init();
                }
                logger.error(e);
            }
        }
    }

    public String getMabstract(String cont) {
        while (true) {
            if (this.client == null) {
                init();
            }
            try {
                ABSHold absHold = new ABSHold(5, 10);
                ABSResult absResult = this.client.ABSText(cont, absHold);
                String tabs = absResult.getabs();
                if ("".equals(tabs) || tabs == null) {
                    return "";
                } else {
                    return tabs;
                }
            } catch (CkmSoapException ex) {
                logger.error("trserror  and continue" + ex);
                String errorStr = ex.toString();
                if (errorStr.contains("http")) {
                    String urlD = errorStr.substring(errorStr.indexOf("http"), errorStr.indexOf("8000") + 4);
                    this.trsUrls = getTrsUrl(this.trsUrls, urlD);
                    this.client = null;
                }
            } catch (Exception e) {
                if (this.client == null) {
                    init();
                }
                logger.error(e);
            }
        }
    }

    public HashMap[] getMKeyWordVector(String cont) {
        while (true) {
            if (this.client == null) {
                init();
            }
            try {
                ABSHold absHold = new ABSHold(5, 10);
                ABSResult absResult = this.client.ABSText(cont, absHold);
                String keywordStr = absResult.getwordlist();
                if ("".equals(keywordStr) || keywordStr == null) {
                    return new HashMap[]{};
                } else {
                    String[] keywords = keywordStr.split(";");
                    HashMap[] keywordsmap = new HashMap[keywords.length];
                    for (int m = 0; m < keywords.length; m++) {
                        HashMap vm = new HashMap();
                        vm.put("v", keywords[m]);
                        vm.put("w", "");
                        keywordsmap[m] = vm;
                    }
                    return keywordsmap;
                }
            } catch (CkmSoapException ex) {
                logger.error("trserror  and continue" + ex);
                String errorStr = ex.toString();
                if (errorStr.contains("http")) {
                    String urlD = errorStr.substring(errorStr.indexOf("http"), errorStr.indexOf("8000") + 4);
                    this.trsUrls = getTrsUrl(this.trsUrls, urlD);
                    this.client = null;
                }
            } catch (Exception e) {
                if (this.client == null) {
                    init();
                }
                logger.error(e);
            }
        }
    }

    public HashMap<String, HashMap[]> getMXXVector(String cont) {
        while (true) {
            if (this.client == null) {
                init();
            }
            try {
                HashMap<String, HashMap[]> vectorMap = new HashMap<String, HashMap[]>();
                PLOResult[] result = this.client.PLOText(cont, 0x0001);
                List<PLOResult> personList = new ArrayList<PLOResult>();
                List<PLOResult> organizationList = new ArrayList<PLOResult>();
                List<PLOResult> regionList = new ArrayList<PLOResult>();
                HashMap[] personmap = null;
                HashMap[] organizationmap = null;
                HashMap[] regionmap = null;
                if (result != null) {
                    for (int m = 0; m < result.length; m++) {
                        switch (result[m].gettype()) {
                            case 1001:
                                personList.add(result[m]);
                                break;
                            case 1003:
                                organizationList.add(result[m]);
                                break;
                            case 1002:
                                regionList.add(result[m]);
                                break;
                            default:
                                break;
                        }
                    }
                    personmap = new HashMap[personList.size()];
                    for (int p = 0; p < personList.size(); p++) {
                        HashMap vm = new HashMap();
                        vm.put("v", personList.get(p).getword());
                        vm.put("w", "");
                        personmap[p] = vm;
                    }

                    organizationmap = new HashMap[organizationList.size()];
                    for (int o = 0; o < organizationList.size(); o++) {
                        HashMap vm = new HashMap();
                        vm.put("v", organizationList.get(o).getword());
                        vm.put("w", "");
                        organizationmap[o] = vm;
                    }

                    regionmap = new HashMap[regionList.size()];
                    for (int r = 0; r < regionList.size(); r++) {
                        HashMap vm = new HashMap();
                        vm.put("v", regionList.get(r).getword());
                        vm.put("w", "");
                        regionmap[r] = vm;
                    }
                } else {
                    personmap = new HashMap[]{};
                    organizationmap = new HashMap[]{};
                    regionmap = new HashMap[]{};
                }
                vectorMap.put("person", personmap);
                vectorMap.put("organization", organizationmap);
                vectorMap.put("region", regionmap);
                return vectorMap;
            } catch (CkmSoapException ex) {
                logger.error("trserror  and continue" + ex);
                String errorStr = ex.toString();
                if (errorStr.contains("http")) {
                    String urlD = errorStr.substring(errorStr.indexOf("http"), errorStr.indexOf("8000") + 4);
                    this.trsUrls = getTrsUrl(this.trsUrls, urlD);
                    this.client = null;
                }
            } catch (Exception e) {
                if (this.client == null) {
                    init();
                }
                logger.error(e);
            }
        }
    }

    public long getMsentiment(String cont) {
        while (true) {
            if (this.client == null) {
                init();
            }
            try {
                long reNumber = -1;
                String resul = this.client.AppraiseParseLN("bb_pingjia_add_LN", cont);
                if (resul == null || "0".equals(resul) || "".equals(resul)) {
                    reNumber = -1;
                } else {
                    Float f = Float.valueOf(resul);
                    if (f > 0 && f <= 100) {
                        reNumber = 100;
                    } else if (f < 0 && f >= -100) {
                        reNumber = 0;
                    }
                }
                return reNumber;
            } catch (CkmSoapException ex) {
                logger.error("trserror  and continue" + ex);
                String errorStr = ex.toString();
                if (errorStr.contains("http")) {
                    String urlD = errorStr.substring(errorStr.indexOf("http"), errorStr.indexOf("8000") + 4);
                    this.trsUrls = getTrsUrl(this.trsUrls, urlD);
                    this.client = null;
                }
            } catch (Exception e) {
                if (this.client == null) {
                    init();
                }
                logger.error(e);
            }
        }
    }
    
//    public long getMImportance(String cont){
//            while (true) {
//            if (this.client == null) {
//                init();
//            }
//            try {
//                long reNumber = -1;
//                String resul = this.client.AppraiseParseLN("bb_pingjia_add_LN", cont);
//                if (resul == null || "0".equals(resul) || "".equals(resul)) {
//                    reNumber = -1;
//                } else {
//                    Float f = Float.valueOf(resul);
//                    if (f > 0 && f <= 100) {
//                        reNumber = 100;
//                    } else if (f < 0 && f >= -100) {
//                        reNumber = 0;
//                    }
//                }
//                return reNumber;
//            } catch (CkmSoapException ex) {
//                logger.error("trserror  and continue" + ex);
//                String errorStr = ex.toString();
//                if (errorStr.contains("http")) {
//                    String urlD = errorStr.substring(errorStr.indexOf("http"), errorStr.indexOf("8000") + 4);
//                    this.trsUrls = getTrsUrl(this.trsUrls, urlD);
//                    this.client = null;
//                }
//            } catch (Exception e) {
//                if (this.client == null) {
//                    init();
//                }
//                logger.error(e);
//            }
//        }
//    }

    public String[] getMCategory_Tag(String cont) {
        while (true) {
            if (this.client == null) {
                init();
            }
            try {
                RuleCATField[] fields = new RuleCATField[1];
                fields[0] = new RuleCATField(cont, "m_content");
                String rslt = this.client.RuleCATClassifyText("CNML_RULE", fields);
                if ("".equals(rslt) || rslt == null) {
                    return new String[]{};
                } else {
                    return rslt.split(";");
                }
            } catch (CkmSoapException ex) {
                logger.error("trserror  and continue" + ex);
                String errorStr = ex.toString();
                if (errorStr.contains("http")) {
                    String urlD = errorStr.substring(errorStr.indexOf("http"), errorStr.indexOf("8000") + 4);
                    this.trsUrls = getTrsUrl(this.trsUrls, urlD);
                    this.client = null;
                }
            } catch (Exception e) {
                if (this.client == null) {
                    init();
                }
                logger.error(e);
            }
        }
    }

    public String[] getMCatagoryIndustry(String cont) {
        while (true) {
            if (this.client == null) {
                init();
            }
            RuleCATField[] fields = new RuleCATField[1];
            fields[0] = new RuleCATField(cont, "m_content");
            try {
                String rslt = this.client.RuleCATClassifyText("内容行业分类", fields);
                if ("".equals(rslt) || rslt == null) {
                    return new String[]{};
                } else {
                    return rslt.split(";");
                }
            } catch (CkmSoapException ex) {
                logger.error("trserror  and continue" + ex);
                String errorStr = ex.toString();
                if (errorStr.contains("http")) {
                    String urlD = errorStr.substring(errorStr.indexOf("http"), errorStr.indexOf("8000") + 4);
                    this.trsUrls = getTrsUrl(this.trsUrls, urlD);
                    this.client = null;
                }
            } catch (Exception e) {
                if (this.client == null) {
                    init();
                }
                logger.error(e);
            }
        }
    }

    public long getMSiteCategory(String cont) {
        while (true) {
            if (this.client == null) {
                init();
            }
            try {
                long reNumber = -1;

                RuleCATField[] fields2 = new RuleCATField[1];
                fields2[0] = new RuleCATField(cont, "m_site_name");
                String rslt2 = this.client.RuleCATClassifyText("行业分类", fields2);
                if ("".equals(rslt2) || rslt2 == null) {
                    reNumber = -1;
                } else {
                    reNumber = Long.parseLong(rslt2);
                }
                return reNumber;
            } catch (CkmSoapException ex) {
                logger.error("trserror  and continue" + ex);
                String errorStr = ex.toString();
                if (errorStr.contains("http")) {
                    String urlD = errorStr.substring(errorStr.indexOf("http"), errorStr.indexOf("8000") + 4);
                    this.trsUrls = getTrsUrl(this.trsUrls, urlD);
                    this.client = null;
                }
            } catch (Exception e) {
                if (this.client == null) {
                    init();
                }
                logger.error(e);
            }
        }
    }

    public static void main(String[] args) {
        String[] sss = null;
        TRSTools trs = new TRSTools();
        long mSiteCategory = trs.getMSiteCategory("人民网强国论坛");
        String[] keywords = trs.getMKeyWordList("国家食品药品监督管理总局药品医疗器械");
        System.out.println(keywords);

        String abs = trs.getMabstract("国家食品药品监督管理总局药品医疗器械");
        System.out.println(abs);
//
//        HashMap[] keywordsmap = trs.getMKeyWordVector("国家食品药品监督管理总局药品医疗器械");
//        System.out.println(keywordsmap);
//
//        HashMap mxxVector = trs.getMXXVector("人民网，日本和中国陕西省的编辑编写本文档的目的在于从习近平总体设计的角度明确本系统的功能和业务流程，明确与其他子系统的接口，使系统开发人员明确系统功能，有针对性的进行系统开发、测试、验收等各方面的工作。本说明书的读者包括：提出该需求的业务处室工作人员IT行业、餐饮行业、系统建设管理部门的工作人员、业务系统开发单位的工作人员、以及负责系统验收运行维护的工作人员、合作开发单位的相关技术人员等,法伦功，共产党，江泽民，中国威胁论。");
//        for (Iterator columnsit = mxxVector.entrySet().iterator(); columnsit.hasNext();) {
//            Map.Entry<String, HashMap[]> entry = (Map.Entry<String, HashMap[]>) columnsit.next();
//            if ("person".equals(entry.getKey())) {
//                System.out.println(entry.getValue());
//            } else if ("organization".equals(entry.getKey())) {
//                System.out.println(entry.getValue());
//            } else if ("region".equals(entry.getKey())) {
//                System.out.println(entry.getValue());
//            }
//        }
//
//        long msentiment = trs.getMsentiment("国家食品药品监督管理总局药品医疗器械");
//        System.out.println(msentiment);
//
//        String[] mCategory_Tag = trs.getMCategory_Tag("国家食品药品监督管理总局药品医疗器械");
//        System.out.println(mCategory_Tag);

//        String[] mCatagoryIndustry = trs.getMCatagoryIndustry("国家食品药品监督管理总局药品医疗器械");
//        System.out.println(mCatagoryIndustry);
//        long mSiteCategory = trs.getMSiteCategory("国家食品药品监督管理总局药品医疗器械");
//        System.out.println(mSiteCategory);
//        String rslt = "";
//        if (rslt == null || "".equals(rslt)) {
//            System.out.println("null");
//        } else {
//            System.out.println("not null");
//        }

    }
}
